/** 水浒传 */
class SHZGameView extends V.GameView {
	/** 开始按钮 */
	private startstopBt:eui.Button;
	/** 比倍按钮 */
	private bbBt:eui.Button;
	/** 最小押注 */
	private minscore:eui.Button;
	/** 最大押注 */
	private maxscore:eui.Button;
	/** 加注 */
	private addscore:eui.Button;
	/** 减注 */
	private subscore:eui.Button;
	/** 我的游戏币 */
	private score:eui.Label;
	/** 压分 */
	private yscore:eui.Label;
	/** 总压分 */
	private zyscore:eui.Label;
	/** 得分 */
	private dscore:eui.Label;
	/** 可压注分 */
	private betScore:number[];
	/** 游戏模式 0水浒传 3比倍 5小玛丽*/
	private gameMode:number;
	/** 每个格子信息 */
	private itemInfo:number[][];
	/** 动画工厂 */
	private jmpFactory:egret.MovieClipDataFactory;
	/** 线图片 */
	private line:eui.Group;
	/** 线数据 */
	private lineData:any;
	/** 飘旗容器 */
	private piaoqi:eui.Group;
	/** 比倍界面 */
	private bbView:SHZGameView2;

	private iconlist;

	public constructor() {
		super();
		this.MAX_PLAYER = 0;
        this.skinName = "SHZGameSkin";
		this.Res = DataConfig.ResConfig[DataConfig.KIND_ID_SHZ];
		this.gameMode = 0;
		this.iconlist = [[17,23,"icon_09_png"],[196,21,"icon_04_png"],[377,25,"icon_02_png"],[553,5,"icon_08_png"],[739,23,"icon_01_png"],[923,23,"icon_03_png"],[1099,23,"icon_09_png"]
			,[1099,123,"icon_06_png"],[1099,221,"icon_04_png"],[1099,324,"icon_02_png"],[1099,424,"icon_01_png"],[1099,524,"icon_07_png"],[1099,624,"icon_09_png"]
			,[916,624,"icon_05_png"],[739,624,"icon_03_png"],[555,624,"icon_01_png"],[375,622,"icon_04_png"],[197,622,"icon_06_png"]
			,[15,622,"icon_09_png"],[15,523,"icon_05_png"],[15,423,"icon_02_png"],[15,323,"icon_01_png"],[15,223,"icon_03_png"],[15,123,"icon_07_png"]
		];
	}

	protected childrenCreated(): void {
		this.initPlayer();
		let mcFactory:egret.MovieClipDataFactory = new egret.MovieClipDataFactory(RES.getRes("dagu_json"), RES.getRes("dagu_png"));
		let dagu:egret.MovieClip = new egret.MovieClip(mcFactory.generateMovieClipData("dagu"));
		dagu.x = 50;
		dagu.y = 10;
		this.addChild(dagu);
		dagu.play(-1);

		this.jmpFactory = new egret.MovieClipDataFactory(RES.getRes("game1_itemJump_json"), RES.getRes("game1_itemJump_png"));
		// let dh:egret.MovieClip = new egret.MovieClip(mcFactory.generateMovieClipData("jump_dadao"));
		// dh.frameRate = 3;
		// dh.y = 200;
		// dh.x = this.width - piaoqi.width;
		// this.addChild(dh);
		// dh.play(-1);
		this.score.text = D.App.userData.score.toString();
		for(let i=0;i<15;i++){
			this["Item"+i].getChildAt(1).mask = this["Item"+i].getChildAt(0);
		}
		// this.bbBt.enabled = false;
		
		this.bbBt.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onbbBt, this);
		this.minscore.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onChangeScore, this);
		this.maxscore.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onChangeScore, this);
		this.addscore.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onChangeScore, this);
		this.subscore.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onChangeScore, this);
		this.startstopBt.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onStartBt, this);
	}

	public updateDisplayList(unscaledWidth, unscaledHeight):void{
		super.updateDisplayList(unscaledWidth, unscaledHeight);

		let pq:egret.MovieClip = null;
		if(this.piaoqi.numChildren > 0) {
			pq = <egret.MovieClip>this.piaoqi.getChildAt(0);
		}else{
			let mcFactory:egret.MovieClipDataFactory = new egret.MovieClipDataFactory(RES.getRes("piaoqi_json"), RES.getRes("piaoqi_png"));
			pq = new egret.MovieClip(mcFactory.generateMovieClipData("piaoqi"));
			this.piaoqi.addChild(pq);
			pq.play(-1);
		}
		pq.scaleX = this.piaoqi.width / 368;
		pq.scaleY = this.piaoqi.height / 175;
	}

	/** 收到网络消息 */
    public onNetData(s:number, d:any):void {
        switch(s){
            case 100:{ // 游戏开始
            
				let jd:any = D.App.b2j(d, [["L","lScore"],['A',"cbItemInfo",3,'A',5,'B'],['B',"cbGameMode"],['B',"cbBounsGameCount"]]);
				this.gameMode = jd.cbGameMode;
				this.itemInfo = jd.cbItemInfo; console.log(jd);
				for(let i=0;i<15;i++){
					let img:eui.Image = this["Item"+i].getChildAt(1);
					img.source = "common_MoveImg_png";
					img.y = 0;
					egret.Tween.get(img).wait(Math.random()*200).to({y:-1100}, 2000).call(this.ShowItem, this, [i, this.itemInfo[(i/5)|0][i%5]]);
				}
				this.dscore.text = jd.lScore;
				if(this.gameMode == 0) WebSocketInstance.getHallSocket().send("9,7");
				else this.lineData = shz.GameLogic.checkLine(this.itemInfo);
				console.log("linedata", this.lineData);
				break;
			}case 101:{ //放弃比倍
				this.startstopBt.skinName = "shz.startBtSkin";
				this.bbBt.enabled = false;
				break;
			}
		}
	}

	/** 播放元素动画 */
	private ShowItem(index:number, item:number):void{
		let dh:egret.MovieClip = new egret.MovieClip(this.jmpFactory.generateMovieClipData("jump_" + shz.GameLogic.item[item]));
		dh.frameRate = 8;
		this["Item"+index].addChild(dh);
		dh.scaleX = this["Item"+index].width / 210;
		dh.scaleY = this["Item"+index].height / 147;
		dh.addEventListener(egret.Event.COMPLETE, this.ItemEnd, this);
		dh.play();
	}
	/** 动画完成 */
	private ItemEnd(e:egret.Event):void{
		let dp:egret.DisplayObject = e.target;
		let i = 0, img:eui.Image = <eui.Image>dp.parent.getChildAt(1);
		for(;i<15;i++){
			if(this["Item" + i] == dp.parent) break;
		}
		dp.parent.removeChild(dp);
		img.y = 0;
		if(this.gameMode == 0) img.source = "common_icon_0" + (this.itemInfo[(i/5)|0][i%5] + 1) + ".png";
		else{
			let fitm = false;
			for(let j=0;j<this.lineData.length;j++){
				for(let m=0;m<this.lineData[j].length-1;m++){
					if(this.lineData[j][m][0] == ((i/5)|0) && this.lineData[j][m][1] == i%5){
						let tp = shz.GameLogic.item[this.itemInfo[(i/5)|0][i%5]];
						fitm = true;
						let mcFactory:egret.MovieClipDataFactory = new egret.MovieClipDataFactory(RES.getRes("shz_a_" + tp + "_json"), RES.getRes("shz_a_" + tp + "_png"));
						let mv:egret.MovieClip = new egret.MovieClip(mcFactory.generateMovieClipData(tp));
						mv.frameRate = 10;
						mv.scaleX = this["Item"+i].width / 210;
						mv.scaleY = this["Item"+i].height / 141;
						this["Item" + i].addChild(mv);
						mv.addEventListener(egret.Event.COMPLETE, this.ItemEnd2, this);
						mv.play();
						break;
					}
				}
				if(fitm) break;
			}
			if(fitm) img.source = "common_icon_0" + (this.itemInfo[(i/5)|0][i%5] + 1) + ".png";
			else img.source = "common_icon_1" + (this.itemInfo[(i/5)|0][i%5] + 1) + ".png";
		}
		if(i == 0){
			if(this.gameMode == 0){
				this.startstopBt.skinName = "shz.startBtSkin";
				this.score.text = D.App.userData.score.toString();
			}else{
				this.bbBt.enabled = true;
				this.startstopBt.skinName = "shz.sfBtSkin";
				if(this.lineData.length > 0){
					this.line.removeChildren();
					this.line.visible = true;
					for(let n=0;n<this.lineData.length;n++){
						let mg:eui.Image = new eui.Image("0" + (this.lineData[n][this.lineData[n].length-1] + 1) + "_png");
						mg.width = this.line.width;
						mg.height = this.line.height;
						this.line.addChild(mg);
					}
				}
			}
		}
	}

	/** 第二个动画完成 */
	public ItemEnd2(e:egret.Event):void{
		let dp:egret.DisplayObject = e.target;
		dp.parent.removeChild(dp);
	}

	/** 同步桌子 */
    public onSyncData(data:any):void{
		D.App.table.waitReady = 1;
		if(D.App.table.gameStatus == 0){
			let jd:any = D.App.b2j(data, [["I","lCellScore"],['B',"cbBetCount"],['A',"lBetScore",8,'L']]);
			this.cellScore = jd.lCellScore;
			this.betScore = jd.lBetScore;
			this.betScore.length = jd.cbBetCount;
			this.yscore.text = jd.lBetScore[0];
			this.zyscore.text = (jd.lBetScore[0] * 9).toString();
		}
	}

	/** 开始游戏 */
	private onStartBt(e:egret.TouchEvent):void{
		if(D.App.userData.userStatus != UserStatus.PLAY) return;
		if(this.startstopBt.skinName == "shz.stopBtSkin") return;
		else if(this.startstopBt.skinName == "shz.sfBtSkin"){
			WebSocketInstance.getHallSocket().send("9,4");
			this.dscore.text = "0";
			return;
		}

		this.line.visible = false;
		this.bbBt.enabled = false;
		this.startstopBt.skinName = "shz.stopBtSkin";
		let d:egret.ByteArray = new egret.ByteArray();
		d.endian = egret.Endian.LITTLE_ENDIAN;
		for(let i=0;i<this.betScore.length;i++){
			if(this.yscore.text == this.betScore[i].toString()){
				d.writeInt(i);
				break;
			}
		}
		d.writeInt(0);
		WebSocketInstance.getHallSocket().send("9,6", d);
	}

	/** 压分修改 */
	private onChangeScore(e:egret.TouchEvent):void{
		let i = 0;
		for(;i<this.betScore.length;i++){
			if(this.yscore.text == this.betScore[i].toString()) break;
		}
		if(e.target == this.minscore){
			i = 0;
		}else if(e.target == this.maxscore){
			i = this.betScore.length - 1;
		}else if(e.target == this.addscore){
			i++;
			if(i >= this.betScore.length) i = this.betScore.length - 1;
		}else if(e.target == this.subscore){
			i--;
			if(i < 0) i = 0;
		}
		this.yscore.text = this.betScore[i].toString();
		this.zyscore.text = (this.betScore[i] * 9).toString();
	}

	/** 打开比倍 */
	private onbbBt(e:egret.TouchEvent):void{
		if(!this.bbView) this.bbView = new SHZGameView2();
		this.addChild(this.bbView);
		this.bbView.yscore.text = this.dscore.text;
	}
}